package com.bdqn.zhang.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {

    private static String driver = "";
    private static String url = "";
    private static String username = "";
    private static String password = "";
    private static Connection conn = null;

    // 静态代码块
    static {
        Properties pro = new Properties();
        try {
            //这是将db.properties数据流放入内存中
            InputStream ins =  Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
            pro.load(ins);
            driver=pro.getProperty("driver");
            url=pro.getProperty("url");
            username=pro.getProperty("username");
            password=pro.getProperty("password");
            ins.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取连接
    public static Connection getConnecton(){
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }


    // 关闭连接的方法
    public void closeAll(ResultSet rs,Statement stmt,Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    // 封装 增 删 改
    public int executeUpdate(String sql, Object[] objs) {
        Connection conn = getConnecton();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            // 绑定参数
            if(objs != null) {
                for (int i = 0; i < objs.length; i++) {
                    pstmt.setObject(i+1, objs[i]);
                }
            }
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(null, pstmt, conn);
        }
        return 0;
    }

    // 封装查询
    public ResultSet executeQuery(String sql, Object[] objs) {
        Connection conn = getConnecton();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            // 绑定参数
            if(objs != null) {
                for (int i = 0; i < objs.length; i++) {
                    pstmt.setObject(i+1, objs[i]);
                }
            }
            ResultSet resultSet = pstmt.executeQuery();
            return resultSet;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(null, pstmt, conn);
        }
        return null;
    }
}

